package com.zhaoyizhe.demo.controller;

import ch.qos.logback.core.util.StringUtil;
import com.zhaoyizhe.demo.constant.ConstantSessionId;
import com.zhaoyizhe.demo.model.UserInfo;
import com.zhaoyizhe.demo.service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @RequestMapping(value = "/login",produces = "application/json")
    public String login(String userName, String password , HttpSession session){
        log.info("UserController.login 接收参数: userName: {} password: {}",userName,password);
        if (StringUtil.isNullOrEmpty(userName) || StringUtil.isNullOrEmpty(password)){
            log.warn("用户名或密码未输入");
            return "用户名或密码未输入";
        }
        UserInfo userInfo = userService.getUserInfo(userName);
        if (userInfo == null) {
            log.warn("用户未注册!");
            return "用户未注册！";
        }
        if (!password.equals(userInfo.getPassword())){
            log.warn("账号或密码错误!");
            return "账号或密码错误!";
        }

        userInfo.setPassword("");
        session.setAttribute(ConstantSessionId.SESSION_ID_KEY,userInfo);
        log.info("登陆成功！");
        return "";
    }

}
